package com.learn.springcloud.second.web.config;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

/**
 * 防止带有 RequestMapping 的feign 被映射成 Spring mvc handler
 * @auth lwt
 * @data 2022/8/25
 */
public class CustRequestMappingHandlerMapping extends RequestMappingHandlerMapping {

    @Override
    protected boolean isHandler(Class<?> beanType) {
        if(AnnotatedElementUtils.hasAnnotation(beanType, Controller.class)){
            return true;
        }
        //带有【FeignClient注解】的候选组件不再查看是否有【RequestMapping注解】
        if(AnnotatedElementUtils.hasAnnotation(beanType, FeignClient.class)){
            return false;
        }
        if(AnnotatedElementUtils.hasAnnotation(beanType, RequestMapping.class)){
            return true;
        }
        return false;
    }
}
